﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Linq;
using System.Threading.Tasks;

namespace Topro.Web.Controllers
{
    using Topro.Application.Interface;
    using Topro.Entity;
    using Topro.Entity.Enum;
    using Topro.Entity.Query;
    using Topro.Entity.Request.Container;
    using Topro.Util;

    /// <summary>
    /// 容器控制器
    /// </summary>
    public class ContainerController : BaseController
    {
        #region 注入
        /// <summary>
        /// 日志接口
        /// </summary>
        private readonly LogService _logService;

        /// <summary>
        /// 容器接口
        /// </summary>
        private readonly ContainerService _containerService;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logService">日志接口</param>
        /// <param name="warehouseService">容器接口</param>
        public ContainerController(LogService logService, ContainerService containerService)
        {
            _logService = logService;
            _containerService = containerService;
        }
        #endregion

        /// <summary>
        /// 列表
        /// </summary>
        /// <returns></returns>
        public new IActionResult Index()
        {
            return View("Index");
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="query">查询条件</param>
        /// <returns>PagedList<?></returns>
        public JsonResult ContainerQuery(ContainerQuery query)
        {
            var list = _containerService.GetPager(query);

            return Json(list);
        }

        /// <summary>
        /// 新增、编辑
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns></returns>
        public IActionResult ContainerAddOrEdit(int? id)
        {
            var viewName = "ContainerEdit";

            var model = _containerService.Get(id);

            if (id.HasValue)
            {
                viewName = "ContainerEdit";
            }

            //状态
            ViewBag.ContainerType = (Dictionary<int, string>)EnumUtil.ToDictionary(typeof(ContainerType));

            return View(viewName, model);
        }

        /// <summary>
        /// 保存、修改
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public JsonResult Save(ContainerSaveRequest request)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                # region 验证
                //编码
                //if (string.IsNullOrEmpty(request.Name))
                //{
                //    throw new Exception("物料名称不能为空.");
                //}
                //var nameReg = new Regex(@"^.{2,40}$");
                //if (!nameReg.IsMatch(request.Name))
                //{
                //    throw new Exception("物料名称必须介于 2 和 40 之间");
                //}
                //排序
                if (string.IsNullOrEmpty(request.DisplayOrder))
                {
                    throw new Exception("排序不能为空.");
                }

                var sortNoReg = new Regex(@"^(([1-9]\d{0,1})|0)?$");
                if (!sortNoReg.IsMatch(request.DisplayOrder))
                {
                    throw new Exception("排序只能输入两位正整数.");
                }
                //状态
                if (!request.Status.HasValue)
                {
                    throw new Exception("选择状态.");
                }

                var status = new List<int>()
                {
                    (int)ContainerStatus.启用,
                    (int)ContainerStatus.禁用
                };

                if (!status.Contains(request.Status.Value))
                {
                    throw new Exception("状态参数错误.");
                }
                #endregion

                //request.Ip = Ip;
                request.CreatedBy = UserId;

                response = _containerService.SaveContainer(request);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <returns></returns>
        public JsonResult UpdateStatus(int id, int status)
        {
            var response = new JResult()
            {
                Success = false
            };

            try
            {
                response = _containerService.UpdateStatus(id, status);
                response.Success = true;
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
            }

            return Json(response);
        }
    }
}
